package com.sky.service.common.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.exception.BaseException;
import com.sky.mapper.common.DishFlavorMapper;
import com.sky.mapper.common.DishMapper;
import com.sky.mapper.common.SetMealDishMapper;
import com.sky.pojo.Dish;
import com.sky.pojo.DishFlavor;
import com.sky.result.PageResult;
import com.sky.service.common.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper  dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetMealDishMapper  setMealDishMapper;

    @Transactional
    @Override
    public void deleteBatch(List<Long> ids) {
        log.info("批量删除：{}", ids);
        if (Objects.isNull(ids) || ids.isEmpty()){
            throw  new RuntimeException("删除菜品id不能为空");
        }
        Long countStatus = dishMapper.countByStatusAndIds(StatusConstant.ENABLE,ids);
        if (countStatus > 0){
            throw new BaseException(MessageConstant.DISH_ON_SALE);
        }
        Long countByDishIds = setMealDishMapper.countByDishIds(ids);
        if (countByDishIds > 0){
            throw new BaseException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        dishMapper.deleteBatch(ids);
        dishFlavorMapper.deleteByDishIds(ids);
    }

    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        log.info("分页查询：{}", dishPageQueryDTO);
        boolean flag = BeanUtil.hasNullField(dishPageQueryDTO, "name",  "categoryId", "status");
        if (flag){
            throw  new RuntimeException("查询参数不能为空");
        }
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> pageListVO = dishMapper.selectPageByNameAndType(dishPageQueryDTO);
        PageResult pageResult = new PageResult<>();
        pageResult.setRecords(pageListVO.getResult());
        pageResult.setTotal(pageListVO.getTotal());
        return pageResult;
    }

    @Transactional
    @Override
    public void addDish(DishDTO dishDTO) {
        log.info("新增菜品：{}",dishDTO);
        boolean flag = BeanUtil.hasNullField(dishDTO, "description", "status", "flavors","id");
        if (flag){
            throw  new RuntimeException("菜品参数不能为空");
        }
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.insert(dish);
        List<DishFlavor> flavorList = dishDTO.getFlavors();
        if (Objects.nonNull(flavorList) && !flavorList.isEmpty()){
            List<DishFlavor> list = flavorList.stream().map(flavor -> {
                DishFlavor dishFlavor = new DishFlavor();
                BeanUtils.copyProperties(flavor, dishFlavor);
                dishFlavor.setDishId(dish.getId());
                return dishFlavor;
            }).collect(Collectors.toList());
            dishFlavorMapper.insertBatch(list);
        }
    }
}
